package com.example.springboot_demo.pojo;


import org.apache.commons.collections.map.LinkedMap;

import java.io.Serializable;
import java.util.Map;

//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//使用 lombok简化开发，@data代替set，get和toString方法
// @AllArgsConstructor代替有参构造方法 @NoArgsConstructor代替无参构造方法
public class Cart<T> implements Serializable {
    //购物车总数量
    private Integer totalCnt = 0;

    //购物车总价格
    private double totalPrice = 0;

    //存放每个商品
    private Map<Integer, CartData<Book>> items = new LinkedMap();

    public Cart() {
    }

    public Cart(Integer totalCnt, double totalPrice, Map<Integer, CartData<Book>> items) {
        this.totalCnt = totalCnt;
        this.totalPrice = totalPrice;
        this.items = items;
    }


    public Integer getTotalCnt() {
        return totalCnt;
    }

    public void setTotalCnt(Integer totalCnt) {
        this.totalCnt = totalCnt;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Map<Integer, CartData<Book>> getItems() {
        return items;
    }

    public void setItems(Map<Integer, CartData<Book>> items) {
        this.items = items;
    }

    //增加一种图书
    public void addCartData_book(CartData<Book> cartData) {
        int id = cartData.getId();
        //查询在购物车中是否已经存在该图书
        CartData<Book> item = items.get(id);
        if (item == null) {
            //图书不存在，将该图书加入购物车
            items.put(id, cartData);
            totalCnt += 1;
            totalPrice += cartData.getBook_UnitPrice();
            totalPrice = Math.round(totalPrice * 100) / 100.00;
        } else {
            //存在该图书直接修改总价格和总数量
            item.setBookCnt(item.getBookCnt() + 1);
            totalCnt += 1;
            item.setBook_allPrice(Math.round(item.getBook_UnitPrice() * item.getBookCnt() * 100) / 100.00);
            totalPrice += item.getBook_UnitPrice();
            totalPrice = Math.round(totalPrice * 100) / 100.00;
        }

    }

    //将图书移除购物车
    public void deleteCartData_book(Integer bookId) {
        totalPrice = totalPrice - items.get(bookId).getBook_allPrice();
        totalPrice = Math.round(totalPrice * 100) / 100.00;
        totalCnt = totalCnt - items.get(bookId).getBookCnt();
        items.remove(bookId);

    }

    //减少已存在的图书的数量
    public void decrease_book(Integer bookId) {
        CartData<Book> item = items.get(bookId);
        if (item.getBookCnt() - 1 != 0) {
            item.setBookCnt(item.getBookCnt() - 1);
            totalCnt = totalCnt - 1;
            item.setBook_allPrice(Math.round(item.getBook_UnitPrice() * item.getBookCnt() * 100) / 100.00);
            totalPrice = totalPrice - item.getBook_UnitPrice();
            totalPrice = Math.round(totalPrice * 100) / 100.00;
        } else {
            totalPrice = totalPrice - items.get(bookId).getBook_allPrice();
            totalPrice = Math.round(totalPrice * 100) / 100.00;
            totalCnt = totalCnt - items.get(bookId).getBookCnt();
            items.remove(bookId);
        }


    }

    //清空购物车
    public void clearCart() {
        totalCnt = 0;
        totalPrice = 0;
        items.clear();
    }
}
